package com.mf.all.controller;

import com.mf.all.entity.dto.TaskDetailBo;
import com.mf.all.entity.req.TaskDetailReq;
import com.mf.all.service.ITaskDetailService;
import com.mf.all.service.ITaskService;
import com.mf.all.groups.AddGroupsInterface;
import com.mf.all.groups.UpdateGroupsInterface;
import com.mf.all.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 任务汇报明细
 *
 * @author mf
 * @since 2025-03-08
 */
@RestController
@RequestMapping("taskDetail/v1")
@Tag(name = "任务汇报明细")
public class TaskDetailController {
    @Resource
    private ITaskDetailService taskDetailService;

    @Resource
    private ITaskService taskService;

    @Operation(summary = "新增任务汇报明细")
    @PostMapping("save")
    public Result<Boolean> save(@Validated(AddGroupsInterface.class) @RequestBody TaskDetailReq taskDetailReq) {
        taskService.verifyTaskStatus(taskDetailReq.getTaskId());
        return Result.success(taskDetailService.save(setTaskDetailBo(taskDetailReq)));
    }

    @Operation(summary = "编辑任务汇报")
    @PostMapping("update")
    public Result<Boolean> update(@Validated(UpdateGroupsInterface.class) @RequestBody TaskDetailReq taskDetailReq) {
        taskService.verifyTaskStatus(taskDetailReq.getTaskId());
        return Result.success(taskDetailService.updateById(setTaskDetailBo(taskDetailReq)));
    }

    @Operation(summary = "删除任务明细汇报")
    @GetMapping("delete/{id}/{taskId}")
    public Result<Boolean> delete(@PathVariable("id") int id, @PathVariable("taskId") int taskId) {
        taskService.verifyTaskStatus(taskId);
        return Result.success(taskDetailService.removeById(id));
    }

    @Operation(summary = "查询任务id查询汇报明细")
    @GetMapping("findList/{taskId}")
    public Result<List<TaskDetailBo>> findList(@PathVariable("taskId") int taskId) {
        return Result.success(taskDetailService.findListById(taskId));
    }

    private TaskDetailBo setTaskDetailBo(TaskDetailReq taskDetailReq) {
        TaskDetailBo taskDetailBo = new TaskDetailBo();
        BeanUtils.copyProperties(taskDetailReq, taskDetailBo);
        return taskDetailBo;
    }

}
